package com.lyrcsoft.config;


import com.lyrcsoft.advice.MyMethodBeforeAdvice;
import com.lyrcsoft.services.UserService;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;

@Configurable
@ComponentScan("com.lyrcsoft")
//@Import(DefaultAdvisorAutoProxyCreator.class)
@Import(AnnotationAwareAspectJAutoProxyCreator.class)
public class AppConfig {


	/**
	 * 定义我们需要的Advisor，前面说过，一个Advisor包含一个Pointcut和Advice。
	 * <p>
	 * 方法中，切了test方法。添加了{@link MyMethodBeforeAdvice} 通知
	 * </p>
	 *
	 * @return
	 */
	//@Bean
	//public DefaultPointcutAdvisor defaultPointcutAdvisor() {
	//	DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
	//	// 通过BeanName定义切点
	//	NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
	//	// 设置切点的方法名称
	//	pointcut.addMethodName("test");
	//	defaultPointcutAdvisor.setPointcut(pointcut);
	//	defaultPointcutAdvisor.setAdvice(new MyMethodBeforeAdvice());
	//	return defaultPointcutAdvisor;
	//}

	/**
	 * 这是一个BeanPostProcessor，Bean的生命周期会走这个后置处理器
	 * <p>
	 *     这行代码可以简化一下。通过@Import(DefaultAdvisorAutoProxyCreator.class)
	 * </p>
	 *
	 * @return
	 */
	//@Bean
	//public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
	//	DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	//	return defaultAdvisorAutoProxyCreator;
	//}
	/**
	 * 定义的这个Bean是BeanPostProcessor
	 *
	 * @return
	 */
	//@Bean
	//public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
	//	// 构建的是BeanPostProcessor，那么就会走Bean的生命周期，在走Bean的生命周期的时候，就开源通过传过来的Bean
	//	// 与配置的BeanNameAutoProxyCreator参数，设置代理逻辑。
	//	BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
	//	// 设置需要拦截的BeanName
	//	beanNameAutoProxyCreator.setBeanNames("userSer*");
	//	// 指定拦截器的BeanName
	//	beanNameAutoProxyCreator.setInterceptorNames("myMethodBeforeAdvice");
	//	beanNameAutoProxyCreator.setProxyTargetClass(true);
	//	return beanNameAutoProxyCreator;
	//	//
	//}
}
